package com.shd.edanpei.presenter;


import com.shd.edanpei.bean.UserBean;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.presenter.contract.EditContract;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.db.DaoManager;

import java.util.WeakHashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/4/4.
 * yang-wen-wen
 */

public class EditPresenter extends BasePresenterImpl<EditContract.EditIView>
        implements EditContract.EditIPresenter {

    public EditPresenter(EditContract.EditIView mView) {
        super(mView);
    }

    public void update(String key, final String value) {
        WeakHashMap<String, Object> weakHashMap = new WeakHashMap<>();
        weakHashMap.put(key, value);
        ApiCreator.getApi().updateUserInfo(weakHashMap)
                .compose(RxTransformer.<UserBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UserBean>() {
                    @Override
                    protected void onSuccess(UserBean data) {
                        DaoManager.getInstance().setNickName(data.getNickName());
                        mBaseIView.updateSuccess(value);
                    }
                });

    }
}
